package com.web.exiaodao.remote.service.impl;

import com.web.exiaodao.business.entity.*;
import com.web.exiaodao.remote.vo.ShopStandardValuesVo;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.repository.ShopStandardValuesRepository;
import com.web.exiaodao.remote.service.ApiShopStandardValuesService;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.XaUtil;

/**
 * @author: zhangl
 * @times: 2016-11-30 11:18:19
 * 类的说明：前端APIShopStandardValues接口实现类
 **/
@Service("ApiShopStandardValuesService")
@Transactional(readOnly = false)
public class ApiShopStandardValuesServiceImpl extends BaseService<ShopStandardValues> implements ApiShopStandardValuesService {

    private static final Logger LOGGER = Logger.getLogger(ApiShopStandardValuesServiceImpl.class);

    @Autowired
    ShopStandardValuesRepository shopStandardValuesRepository;

    @Override
    public XaResult<ShopStandardValuesVo> findOne(Long tId) throws BusinessException {
        ShopStandardValues obj = shopStandardValuesRepository.findByIdAndStatusNot(tId,XaConstant.Status.delete);
        XaResult<ShopStandardValuesVo> xr = new XaResult<ShopStandardValuesVo>();
        if (XaUtil.isNotEmpty(obj)) {
            xr.setObject(JSON.parseObject(JSON.toJSONString(obj),ShopStandardValuesVo.class));
            xr.getObject().setId(obj.getId());
        } else {
            throw new BusinessException(XaConstant.Message.object_not_find);
        }
        return xr;
    }

    @Override
    public XaResult<List<ShopStandardValuesVo>> findListNEStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
        if (status == null) {// 默认显示非删除的所有数据
            status = XaConstant.Status.delete;
        }
        filters.put("status", new SearchFilter("status", Operator.NE, status));
        Page<ShopStandardValues> page = shopStandardValuesRepository.findAll(DynamicSpecifications
                .bySearchFilter(filters.values(), ShopStandardValues.class), pageable);
        XaResult<List<ShopStandardValuesVo>> xr = new XaResult<List<ShopStandardValuesVo>>();
        xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), ShopStandardValuesVo.class));
        for (int i = 0; i < page.getContent().size(); i++) {
            xr.getObject().get(i).setId(page.getContent().get(i).getId());
        }
        return xr;
    }

    @Override
    public XaResult<List<ShopStandardValuesVo>> findListEQStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

    @Override
    public XaResult<ShopStandardValuesVo> multiOperate(String modelIds,
                                                       Integer status) throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

    @Override
    public XaResult<ShopStandardValuesVo> createModel(ShopStandardValues model)
            throws BusinessException {
        XaResult<ShopStandardValuesVo> xr = new XaResult<ShopStandardValuesVo>();
        ShopStandardValues obj = shopStandardValuesRepository.save(model);
        xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ShopStandardValuesVo.class));
        xr.getObject().setId(obj.getId());
        LOGGER.info("新增数据成功----->ShopStandardValues");
        return xr;
    }

    @Override
    public XaResult<ShopStandardValuesVo> updateModel(ShopStandardValues model)
            throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

}
